<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>sm4加解密 | 清峰小栈</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="sm4" />
  
  
  
  
  <meta name="description" content="SM4加解密（前后端）java与js前后端加解密（亲测有效） 1、Sm4工具类12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485">
<meta property="og:type" content="article">
<meta property="og:title" content="SM4加解密">
<meta property="og:url" content="http://example.com/2021/06/28/Springboot/SM4%E5%8A%A0%E8%A7%A3%E5%AF%86%EF%BC%88%E5%89%8D%E5%90%8E%E7%AB%AF%EF%BC%89/index.html">
<meta property="og:site_name" content="清峰小栈">
<meta property="og:description" content="SM4加解密（前后端）java与js前后端加解密（亲测有效） 1、Sm4工具类12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-06-28T02:51:10.000Z">
<meta property="article:modified_time" content="2023-05-04T01:22:34.216Z">
<meta property="article:author" content="清峰">
<meta property="article:tag" content="sm4">
<meta name="twitter:card" content="summary">
  

  

  <link rel="icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  <link rel="apple-touch-icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <!-- <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> -->
  <link href="/qingfeng-blog/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("/qingfeng-blog/css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("/qingfeng-blog/css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("/qingfeng-blog/css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  
<link rel="stylesheet" href="/qingfeng-blog/css/style.css">


  
<script src="/qingfeng-blog/js/jquery-3.1.1.min.js"></script>

  
<script src="/qingfeng-blog/js/bootstrap.js"></script>


  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/qingfeng-blog/css/bootstrap.css" >

  
    
<link rel="stylesheet" href="/qingfeng-blog/css/dialog.css">

  

  

  
    <link rel="stylesheet" href="/qingfeng-blog/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/qingfeng-blog/css/vdonate.css" >
  

<meta name="generator" content="Hexo 6.3.0"></head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="155px" height="90px" alt="Hike News" src="/qingfeng-blog/css/images/mylogo-2.png">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/tags">标签</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="Type something..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/qingfeng-blog/',
        CONTENT_URL: '/qingfeng-blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/qingfeng-blog/js/insight.js"></script>


</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Springboot/SM4加解密（前后端）" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
<div id="container">
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      SM4加解密
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/qingfeng-blog/2021/06/28/Springboot/SM4%E5%8A%A0%E8%A7%A3%E5%AF%86%EF%BC%88%E5%89%8D%E5%90%8E%E7%AB%AF%EF%BC%89/" class="article-date">
	  <time datetime="2021-06-28T02:51:10.000Z" itemprop="datePublished">2021-06-28</time>
	</a>

      
    <a class="article-category-link" href="/qingfeng-blog/categories/Java/">Java</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		PV:<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="SM4加解密（前后端）"><a href="#SM4加解密（前后端）" class="headerlink" title="SM4加解密（前后端）"></a>SM4加解密（前后端）</h1><p>java与js前后端加解密（亲测有效）</p>
<h2 id="1、Sm4工具类"><a href="#1、Sm4工具类" class="headerlink" title="1、Sm4工具类"></a>1、Sm4工具类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.security.SecureRandom;</span><br><span class="line"><span class="keyword">import</span> java.security.Security;</span><br><span class="line"><span class="keyword">import</span> javax.crypto.Cipher;</span><br><span class="line"><span class="keyword">import</span> javax.crypto.KeyGenerator;</span><br><span class="line"><span class="keyword">import</span> javax.crypto.spec.SecretKeySpec;</span><br><span class="line"><span class="keyword">import</span> org.bouncycastle.jce.provider.BouncyCastleProvider;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 国密SM4分组密码算法工具类（对称加密）</span></span><br><span class="line"><span class="comment"> *&lt;p&gt;GB/T 32907-2016 信息安全技术 SM4分组密码算法&lt;/p&gt;</span></span><br><span class="line"><span class="comment">*&lt;p&gt;SM4-ECB-PKCS5Padding&lt;/p&gt;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Sm4Util</span> &#123;</span><br><span class="line">  <span class="comment">// &lt;!-- 轻量级加密API --&gt;</span></span><br><span class="line">  <span class="comment">//  &lt;dependency&gt;</span></span><br><span class="line">  <span class="comment">//    &lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;</span></span><br><span class="line">  <span class="comment">//    &lt;artifactId&gt;bcprov-jdk15on&lt;/artifactId&gt;</span></span><br><span class="line">  <span class="comment">//&lt;version&gt;1.68&lt;/version&gt;</span></span><br><span class="line">  <span class="comment">//  &lt;/dependency&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> StringALGORITHM_NAME= <span class="string">&quot;SM4&quot;</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> StringALGORITHM_ECB_PKCS5PADDING= <span class="string">&quot;SM4/ECB/PKCS5Padding&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//密钥</span></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> StringKEY_STR= <span class="string">&quot;FA171555405706F73D7B973DB89F0B47&quot;</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">byte</span>[]KEY;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//初始化密钥（十六进制字符串转字节数组）</span></span><br><span class="line">  <span class="keyword">static</span> &#123;</span><br><span class="line">KEY= StrUtil.hexToBin(KEY_STR);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * SM4算法目前只支持128位（即密钥16字节）</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> intDEFAULT_KEY_SIZE= <span class="number">128</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">static</span> &#123;</span><br><span class="line">    <span class="comment">// 防止内存中出现多次BouncyCastleProvider的实例</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="literal">null</span> == Security.getProvider(BouncyCastleProvider.PROVIDER_NAME)) &#123;</span><br><span class="line">      Security.addProvider(<span class="keyword">new</span> <span class="title class_">BouncyCastleProvider</span>());</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="title function_">Sm4Util</span><span class="params">()</span> &#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="comment">//加密</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">txt</span> <span class="operator">=</span> <span class="string">&quot;sm4对称加密&lt;pkcs5&gt;演示←←&quot;</span>;</span><br><span class="line">    <span class="type">byte</span>[] output = Sm4Util.encrypt(StrUtil.strToBin(txt),KEY); <span class="comment">//密文</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">hex</span> <span class="operator">=</span> StrUtil.binToHex(output); <span class="comment">//字节数组转十六进制字符串</span></span><br><span class="line">    System.out.printf(<span class="string">&quot;SM4-ECB-PKCS5Padding，加密输出HEX = %s \\n&quot;</span>, hex);</span><br><span class="line">    <span class="comment">// 解密</span></span><br><span class="line">    <span class="type">byte</span>[] input = StrUtil.hexToBin(hex); <span class="comment">//密文转字节</span></span><br><span class="line">    output = Sm4Util.decrypt(input,KEY); <span class="comment">//字节解密</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> StrUtil.binToStr(output); <span class="comment">//原文字节数组转UTF8字符串</span></span><br><span class="line">    System.out.printf(<span class="string">&quot;SM4-ECB-PKCS5Padding，解密输出 = %s \\n&quot;</span>, s);</span><br><span class="line">    System.out.printf(<span class="string">&quot;SM4-ECB-PKCS5Padding，key = %s&quot;</span>, StrUtil.binToHex(KEY));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 生成密钥</span></span><br><span class="line"><span class="comment">   *&lt;p&gt;建议使用DigestUtil.binToHex将二进制转成HEX&lt;/p&gt;</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>密钥16位</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@throwsException</span> 生成密钥异常</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">byte</span>[] generateKey() <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="type">KeyGenerator</span> <span class="variable">kg</span> <span class="operator">=</span> KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);</span><br><span class="line">    kg.init(DEFAULT_KEY_SIZE, <span class="keyword">new</span> <span class="title class_">SecureRandom</span>());</span><br><span class="line">    <span class="keyword">return</span> kg.generateKey().getEncoded();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 加密，SM4-ECB-PKCS5Padding</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramdata</span>要加密的明文</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramkey</span>密钥16字节，使用Sm4Util.generateKey()生成</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>加密后的密文</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@throwsException</span> 加密异常</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">byte</span>[] encrypt(<span class="type">byte</span>[] data, <span class="type">byte</span>[] key) <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    returnsm4(data, key, Cipher.ENCRYPT_MODE);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 解密，SM4-ECB-PKCS5Padding</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramdata</span>要解密的密文</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramkey</span>密钥16字节，使用Sm4Util.generateKey()生成</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>解密后的明文</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@throwsException</span> 解密异常</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">byte</span>[] decrypt(<span class="type">byte</span>[] data, <span class="type">byte</span>[] key) <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    returnsm4(data, key, Cipher.DECRYPT_MODE);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * SM4对称加解密</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paraminput</span>明文（加密模式）或密文（解密模式）</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramkey</span>密钥</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@parammodeCipher</span>.ENCRYPT_MODE - 加密；Cipher.DECRYPT_MODE - 解密</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>密文（加密模式）或明文（解密模式）</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@throwsException</span> 加解密异常</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">byte</span>[] sm4(<span class="type">byte</span>[] input, <span class="type">byte</span>[] key, <span class="type">int</span> mode)</span><br><span class="line">      <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="type">SecretKeySpec</span> <span class="variable">sm4Key</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SecretKeySpec</span>(key,ALGORITHM_NAME);</span><br><span class="line">    <span class="type">Cipher</span> <span class="variable">cipher</span> <span class="operator">=</span> Cipher</span><br><span class="line">        .getInstance(ALGORITHM_ECB_PKCS5PADDING, BouncyCastleProvider.PROVIDER_NAME);</span><br><span class="line">    cipher.init(mode, sm4Key);</span><br><span class="line">    <span class="keyword">return</span> cipher.doFinal(input);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2、字符串工具类"><a href="#2、字符串工具类" class="headerlink" title="2、字符串工具类"></a>2、字符串工具类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.nio.charset.StandardCharsets;</span><br><span class="line"><span class="keyword">import</span> org.bouncycastle.util.Strings;</span><br><span class="line"><span class="keyword">import</span> org.bouncycastle.util.encoders.Hex;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 字符串工具类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *<span class="doctag">@authorBBF</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StrUtil</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 字节数组转十六进制字符串</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@parambytes</span>字节数组</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>十六进制字符串</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">binToHex</span><span class="params">(<span class="type">byte</span>[] bytes)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> Hex.toHexString(bytes).toUpperCase();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 十六进制字符串转字节数组</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramhex</span>字节数组</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>十六进制字符串</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">byte</span>[] hexToBin(String hex) &#123;</span><br><span class="line">    <span class="keyword">return</span> Hex.decode(hex);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 字节数组转UTF8字符串</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@parambytes</span>字节数组</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@returnUTF</span>8字符串</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">binToStr</span><span class="params">(<span class="type">byte</span>[] bytes)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">String</span>(bytes, StandardCharsets.UTF_8);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * UTF8字符串转字节数组</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@paramstrUTF</span>8字符串</span></span><br><span class="line"><span class="comment">   *<span class="doctag">@return</span>字节数组</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">byte</span>[] strToBin(String str) &#123;</span><br><span class="line">    <span class="keyword">return</span> Strings.toUTF8ByteArray(str);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3、sm-js"><a href="#3、sm-js" class="headerlink" title="3、sm.js"></a>3、sm.js</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">window.sm4=function(r)&#123;function <span class="title function_">n</span><span class="params">(e)</span>&#123;<span class="keyword">if</span>(t[e])<span class="keyword">return</span> t[e].<span class="keyword">exports</span>;<span class="keyword">var</span> o=t[e]=&#123;i:e,l:!<span class="number">1</span>,<span class="keyword">exports</span>:&#123;&#125;&#125;;<span class="keyword">return</span> r[e].call(o.<span class="keyword">exports</span>,o,o.<span class="keyword">exports</span>,n),o.l=!<span class="number">0</span>,o.<span class="keyword">exports</span>&#125;<span class="keyword">var</span> t=&#123;&#125;;<span class="keyword">return</span> n.m=r,n.c=t,n.d=function(r,t,e)&#123;n.o(r,t)||Object.defineProperty(r,t,&#123;configurable:!<span class="number">1</span>,enumerable:!<span class="number">0</span>,get:e&#125;)&#125;,n.n=function(r)&#123;<span class="keyword">var</span> t=r&amp;&amp;r.__esModule?function()&#123;<span class="keyword">return</span> r.<span class="keyword">default</span>&#125;:function()&#123;<span class="keyword">return</span> r&#125;;<span class="keyword">return</span> n.d(t,<span class="string">&quot;a&quot;</span>,t),t&#125;,n.o=function(r,n)&#123;<span class="keyword">return</span> Object.prototype.hasOwnProperty.call(r,n)&#125;,n.p=<span class="string">&quot;&quot;</span>,n(n.s=<span class="number">8</span>)&#125;(&#123;<span class="number">8</span>:function(r,n,t)&#123;<span class="string">&quot;use strict&quot;</span>;function <span class="title function_">e</span><span class="params">(r)</span>&#123;<span class="keyword">if</span>(Array.isArray(r))&#123;<span class="keyword">for</span>(<span class="keyword">var</span> n=<span class="number">0</span>,t=Array(r.length);n&lt;r.length;n++)t[n]=r[n];<span class="keyword">return</span> t&#125;<span class="keyword">return</span> Array.from(r)&#125;function <span class="title function_">o</span><span class="params">(r)</span>&#123;<span class="keyword">for</span>(<span class="keyword">var</span> n=[],t=<span class="number">0</span>,e=r.length;t&lt;e;t+=<span class="number">2</span>)n.push(parseInt(r.substr(t,<span class="number">2</span>),<span class="number">16</span>));<span class="keyword">return</span> n&#125;function <span class="title function_">u</span><span class="params">(r)</span>&#123;<span class="keyword">return</span> r.map(function(r)&#123;<span class="keyword">return</span> r=r.toString(<span class="number">16</span>),<span class="number">1</span>===r.length?<span class="string">&quot;0&quot;</span>+r:r&#125;).join(<span class="string">&quot;&quot;</span>)&#125;function <span class="title function_">i</span><span class="params">(r)</span>&#123;<span class="keyword">for</span>(<span class="keyword">var</span> n=[],t=<span class="number">0</span>,e=r.length;t&lt;e;t++)&#123;<span class="keyword">var</span> o=r.charCodeAt(t);o&lt;=<span class="number">127</span>?n.push(o):o&lt;=<span class="number">2047</span>?(n.push(<span class="number">192</span>|o&gt;&gt;&gt;<span class="number">6</span>),n.push(<span class="number">128</span>|<span class="number">63</span>&amp;o)):(n.push(<span class="number">224</span>|o&gt;&gt;&gt;<span class="number">12</span>),n.push(<span class="number">128</span>|o&gt;&gt;&gt;<span class="number">6</span>&amp;<span class="number">63</span>),n.push(<span class="number">128</span>|<span class="number">63</span>&amp;o))&#125;<span class="keyword">return</span> n&#125;function <span class="title function_">f</span><span class="params">(r)</span>&#123;<span class="keyword">for</span>(<span class="keyword">var</span> n=[],t=<span class="number">0</span>,e=r.length;t&lt;e;t++)r[t]&gt;=<span class="number">224</span>&amp;&amp;r[t]&lt;=<span class="number">239</span>?(n.push(String.fromCharCode(((<span class="number">15</span>&amp;r[t])&lt;&lt;<span class="number">12</span>)+((<span class="number">63</span>&amp;r[t+<span class="number">1</span>])&lt;&lt;<span class="number">6</span>)+(<span class="number">63</span>&amp;r[t+<span class="number">2</span>]))),t+=<span class="number">2</span>):r[t]&gt;=<span class="number">192</span>&amp;&amp;r[t]&lt;=<span class="number">223</span>?(n.push(String.fromCharCode(((<span class="number">31</span>&amp;r[t])&lt;&lt;<span class="number">6</span>)+(<span class="number">63</span>&amp;r[t+<span class="number">1</span>]))),t++):n.push(String.fromCharCode(r[t]));<span class="keyword">return</span> n.join(<span class="string">&quot;&quot;</span>)&#125;function <span class="title function_">a</span><span class="params">(r,n)</span>&#123;<span class="keyword">return</span> r&lt;&lt;n|r&gt;&gt;&gt;<span class="number">32</span>-n&#125;function <span class="title function_">c</span><span class="params">(r)</span>&#123;<span class="keyword">return</span>(<span class="number">255</span>&amp;w[r&gt;&gt;&gt;<span class="number">24</span>&amp;<span class="number">255</span>])&lt;&lt;<span class="number">24</span>|(<span class="number">255</span>&amp;w[r&gt;&gt;&gt;<span class="number">16</span>&amp;<span class="number">255</span>])&lt;&lt;<span class="number">16</span>|(<span class="number">255</span>&amp;w[r&gt;&gt;&gt;<span class="number">8</span>&amp;<span class="number">255</span>])&lt;&lt;<span class="number">8</span>|<span class="number">255</span>&amp;w[<span class="number">255</span>&amp;r]&#125;function <span class="title function_">s</span><span class="params">(r)</span>&#123;<span class="keyword">return</span> r^a(r,<span class="number">2</span>)^a(r,<span class="number">10</span>)^a(r,<span class="number">18</span>)^a(r,<span class="number">24</span>)&#125;function <span class="title function_">p</span><span class="params">(r)</span>&#123;<span class="keyword">return</span> r^a(r,<span class="number">13</span>)^a(r,<span class="number">23</span>)&#125;function <span class="title function_">h</span><span class="params">(r,n,t)</span>&#123;<span class="keyword">for</span>(<span class="keyword">var</span> e=<span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">4</span>),o=<span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">4</span>),u=<span class="number">0</span>;u&lt;<span class="number">4</span>;u++)o[<span class="number">0</span>]=<span class="number">255</span>&amp;r[<span class="number">0</span>+<span class="number">4</span>*u],o[<span class="number">1</span>]=<span class="number">255</span>&amp;r[<span class="number">1</span>+<span class="number">4</span>*u],o[<span class="number">2</span>]=<span class="number">255</span>&amp;r[<span class="number">2</span>+<span class="number">4</span>*u],o[<span class="number">3</span>]=<span class="number">255</span>&amp;r[<span class="number">3</span>+<span class="number">4</span>*u],e[u]=o[<span class="number">0</span>]&lt;&lt;<span class="number">24</span>|o[<span class="number">1</span>]&lt;&lt;<span class="number">16</span>|o[<span class="number">2</span>]&lt;&lt;<span class="number">8</span>|o[<span class="number">3</span>];<span class="keyword">for</span>(<span class="keyword">var</span> i,f=<span class="number">0</span>;f&lt;<span class="number">32</span>;f+=<span class="number">4</span>)i=e[<span class="number">1</span>]^e[<span class="number">2</span>]^e[<span class="number">3</span>]^t[f+<span class="number">0</span>],e[<span class="number">0</span>]^=s(c(i)),i=e[<span class="number">2</span>]^e[<span class="number">3</span>]^e[<span class="number">0</span>]^t[f+<span class="number">1</span>],e[<span class="number">1</span>]^=s(c(i)),i=e[<span class="number">3</span>]^e[<span class="number">0</span>]^e[<span class="number">1</span>]^t[f+<span class="number">2</span>],e[<span class="number">2</span>]^=s(c(i)),i=e[<span class="number">0</span>]^e[<span class="number">1</span>]^e[<span class="number">2</span>]^t[f+<span class="number">3</span>],e[<span class="number">3</span>]^=s(c(i));<span class="keyword">for</span>(<span class="keyword">var</span> a=<span class="number">0</span>;a&lt;<span class="number">16</span>;a+=<span class="number">4</span>)n[a]=e[<span class="number">3</span>-a/<span class="number">4</span>]&gt;&gt;&gt;<span class="number">24</span>&amp;<span class="number">255</span>,n[a+<span class="number">1</span>]=e[<span class="number">3</span>-a/<span class="number">4</span>]&gt;&gt;&gt;<span class="number">16</span>&amp;<span class="number">255</span>,n[a+<span class="number">2</span>]=e[<span class="number">3</span>-a/<span class="number">4</span>]&gt;&gt;&gt;<span class="number">8</span>&amp;<span class="number">255</span>,n[a+<span class="number">3</span>]=<span class="number">255</span>&amp;e[<span class="number">3</span>-a/<span class="number">4</span>]&#125;function <span class="title function_">v</span><span class="params">(r,n,t)</span>&#123;<span class="keyword">for</span>(<span class="keyword">var</span> e=<span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">4</span>),o=<span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">4</span>),u=<span class="number">0</span>;u&lt;<span class="number">4</span>;u++)o[<span class="number">0</span>]=<span class="number">255</span>&amp;r[<span class="number">0</span>+<span class="number">4</span>*u],o[<span class="number">1</span>]=<span class="number">255</span>&amp;r[<span class="number">1</span>+<span class="number">4</span>*u],o[<span class="number">2</span>]=<span class="number">255</span>&amp;r[<span class="number">2</span>+<span class="number">4</span>*u],o[<span class="number">3</span>]=<span class="number">255</span>&amp;r[<span class="number">3</span>+<span class="number">4</span>*u],e[u]=o[<span class="number">0</span>]&lt;&lt;<span class="number">24</span>|o[<span class="number">1</span>]&lt;&lt;<span class="number">16</span>|o[<span class="number">2</span>]&lt;&lt;<span class="number">8</span>|o[<span class="number">3</span>];e[<span class="number">0</span>]^=<span class="number">2746333894</span>,e[<span class="number">1</span>]^=<span class="number">1453994832</span>,e[<span class="number">2</span>]^=<span class="number">1736282519</span>,e[<span class="number">3</span>]^=<span class="number">2993693404</span>;<span class="keyword">for</span>(<span class="keyword">var</span> i,f=<span class="number">0</span>;f&lt;<span class="number">32</span>;f+=<span class="number">4</span>)i=e[<span class="number">1</span>]^e[<span class="number">2</span>]^e[<span class="number">3</span>]^A[f+<span class="number">0</span>],n[f+<span class="number">0</span>]=e[<span class="number">0</span>]^=p(c(i)),i=e[<span class="number">2</span>]^e[<span class="number">3</span>]^e[<span class="number">0</span>]^A[f+<span class="number">1</span>],n[f+<span class="number">1</span>]=e[<span class="number">1</span>]^=p(c(i)),i=e[<span class="number">3</span>]^e[<span class="number">0</span>]^e[<span class="number">1</span>]^A[f+<span class="number">2</span>],n[f+<span class="number">2</span>]=e[<span class="number">2</span>]^=p(c(i)),i=e[<span class="number">0</span>]^e[<span class="number">1</span>]^e[<span class="number">2</span>]^A[f+<span class="number">3</span>],n[f+<span class="number">3</span>]=e[<span class="number">3</span>]^=p(c(i));<span class="keyword">if</span>(t===g)<span class="keyword">for</span>(<span class="keyword">var</span> a,s=<span class="number">0</span>;s&lt;<span class="number">16</span>;s++)a=n[s],n[s]=n[<span class="number">31</span>-s],n[<span class="number">31</span>-s]=a&#125;function <span class="title function_">l</span><span class="params">(r,n,t)</span>&#123;<span class="keyword">var</span> a=arguments.length&gt;<span class="number">3</span>&amp;&amp;<span class="keyword">void</span> <span class="number">0</span>!==arguments[<span class="number">3</span>]?arguments[<span class="number">3</span>]:&#123;&#125;,c=a.padding,s=<span class="keyword">void</span> <span class="number">0</span>===c?<span class="string">&quot;pkcs#5&quot;</span>:c,p=(a.mode,a.output),l=<span class="keyword">void</span> <span class="number">0</span>===p?<span class="string">&quot;string&quot;</span>:p;<span class="keyword">if</span>(<span class="string">&quot;string&quot;</span>==typeof n&amp;&amp;(n=o(n)),<span class="number">16</span>!==n.length)<span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&quot;key is invalid&quot;</span>);<span class="keyword">if</span>(r=<span class="string">&quot;string&quot;</span>==typeof r?t!==g?i(r):o(r):[].concat(e(r)),<span class="string">&quot;pkcs#5&quot;</span>===s&amp;&amp;t!==g)<span class="keyword">for</span>(<span class="keyword">var</span> w=d-r.length%d,A=<span class="number">0</span>;A&lt;w;A++)r.push(w);<span class="keyword">var</span> m=<span class="keyword">new</span> <span class="title class_">Array</span>(y);v(n,m,t);<span class="keyword">for</span>(<span class="keyword">var</span> C=[],x=r.length,b=<span class="number">0</span>;x&gt;=d;)&#123;<span class="keyword">var</span> j=r.slice(b,b+<span class="number">16</span>),k=<span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">16</span>);h(j,k,m);<span class="keyword">for</span>(<span class="keyword">var</span> S=<span class="number">0</span>;S&lt;d;S++)C[b+S]=k[S];x-=d,b+=d&#125;<span class="keyword">if</span>(<span class="string">&quot;pkcs#5&quot;</span>===s&amp;&amp;t===g)&#123;<span class="keyword">var</span> O=C[C.length-<span class="number">1</span>];C.splice(C.length-O,O)&#125;<span class="keyword">return</span><span class="string">&quot;array&quot;</span>!==l?t!==g?u(C):f(C):C&#125;<span class="keyword">var</span> g=<span class="number">0</span>,y=<span class="number">32</span>,d=<span class="number">16</span>,w=[<span class="number">214</span>,<span class="number">144</span>,<span class="number">233</span>,<span class="number">254</span>,<span class="number">204</span>,<span class="number">225</span>,<span class="number">61</span>,<span class="number">183</span>,<span class="number">22</span>,<span class="number">182</span>,<span class="number">20</span>,<span class="number">194</span>,<span class="number">40</span>,<span class="number">251</span>,<span class="number">44</span>,<span class="number">5</span>,<span class="number">43</span>,<span class="number">103</span>,<span class="number">154</span>,<span class="number">118</span>,<span class="number">42</span>,<span class="number">190</span>,<span class="number">4</span>,<span class="number">195</span>,<span class="number">170</span>,<span class="number">68</span>,<span class="number">19</span>,<span class="number">38</span>,<span class="number">73</span>,<span class="number">134</span>,<span class="number">6</span>,<span class="number">153</span>,<span class="number">156</span>,<span class="number">66</span>,<span class="number">80</span>,<span class="number">244</span>,<span class="number">145</span>,<span class="number">239</span>,<span class="number">152</span>,<span class="number">122</span>,<span class="number">51</span>,<span class="number">84</span>,<span class="number">11</span>,<span class="number">67</span>,<span class="number">237</span>,<span class="number">207</span>,<span class="number">172</span>,<span class="number">98</span>,<span class="number">228</span>,<span class="number">179</span>,<span class="number">28</span>,<span class="number">169</span>,<span class="number">201</span>,<span class="number">8</span>,<span class="number">232</span>,<span class="number">149</span>,<span class="number">128</span>,<span class="number">223</span>,<span class="number">148</span>,<span class="number">250</span>,<span class="number">117</span>,<span class="number">143</span>,<span class="number">63</span>,<span class="number">166</span>,<span class="number">71</span>,<span class="number">7</span>,<span class="number">167</span>,<span class="number">252</span>,<span class="number">243</span>,<span class="number">115</span>,<span class="number">23</span>,<span class="number">186</span>,<span class="number">131</span>,<span class="number">89</span>,<span class="number">60</span>,<span class="number">25</span>,<span class="number">230</span>,<span class="number">133</span>,<span class="number">79</span>,<span class="number">168</span>,<span class="number">104</span>,<span class="number">107</span>,<span class="number">129</span>,<span class="number">178</span>,<span class="number">113</span>,<span class="number">100</span>,<span class="number">218</span>,<span class="number">139</span>,<span class="number">248</span>,<span class="number">235</span>,<span class="number">15</span>,<span class="number">75</span>,<span class="number">112</span>,<span class="number">86</span>,<span class="number">157</span>,<span class="number">53</span>,<span class="number">30</span>,<span class="number">36</span>,<span class="number">14</span>,<span class="number">94</span>,<span class="number">99</span>,<span class="number">88</span>,<span class="number">209</span>,<span class="number">162</span>,<span class="number">37</span>,<span class="number">34</span>,<span class="number">124</span>,<span class="number">59</span>,<span class="number">1</span>,<span class="number">33</span>,<span class="number">120</span>,<span class="number">135</span>,<span class="number">212</span>,<span class="number">0</span>,<span class="number">70</span>,<span class="number">87</span>,<span class="number">159</span>,<span class="number">211</span>,<span class="number">39</span>,<span class="number">82</span>,<span class="number">76</span>,<span class="number">54</span>,<span class="number">2</span>,<span class="number">231</span>,<span class="number">160</span>,<span class="number">196</span>,<span class="number">200</span>,<span class="number">158</span>,<span class="number">234</span>,<span class="number">191</span>,<span class="number">138</span>,<span class="number">210</span>,<span class="number">64</span>,<span class="number">199</span>,<span class="number">56</span>,<span class="number">181</span>,<span class="number">163</span>,<span class="number">247</span>,<span class="number">242</span>,<span class="number">206</span>,<span class="number">249</span>,<span class="number">97</span>,<span class="number">21</span>,<span class="number">161</span>,<span class="number">224</span>,<span class="number">174</span>,<span class="number">93</span>,<span class="number">164</span>,<span class="number">155</span>,<span class="number">52</span>,<span class="number">26</span>,<span class="number">85</span>,<span class="number">173</span>,<span class="number">147</span>,<span class="number">50</span>,<span class="number">48</span>,<span class="number">245</span>,<span class="number">140</span>,<span class="number">177</span>,<span class="number">227</span>,<span class="number">29</span>,<span class="number">246</span>,<span class="number">226</span>,<span class="number">46</span>,<span class="number">130</span>,<span class="number">102</span>,<span class="number">202</span>,<span class="number">96</span>,<span class="number">192</span>,<span class="number">41</span>,<span class="number">35</span>,<span class="number">171</span>,<span class="number">13</span>,<span class="number">83</span>,<span class="number">78</span>,<span class="number">111</span>,<span class="number">213</span>,<span class="number">219</span>,<span class="number">55</span>,<span class="number">69</span>,<span class="number">222</span>,<span class="number">253</span>,<span class="number">142</span>,<span class="number">47</span>,<span class="number">3</span>,<span class="number">255</span>,<span class="number">106</span>,<span class="number">114</span>,<span class="number">109</span>,<span class="number">108</span>,<span class="number">91</span>,<span class="number">81</span>,<span class="number">141</span>,<span class="number">27</span>,<span class="number">175</span>,<span class="number">146</span>,<span class="number">187</span>,<span class="number">221</span>,<span class="number">188</span>,<span class="number">127</span>,<span class="number">17</span>,<span class="number">217</span>,<span class="number">92</span>,<span class="number">65</span>,<span class="number">31</span>,<span class="number">16</span>,<span class="number">90</span>,<span class="number">216</span>,<span class="number">10</span>,<span class="number">193</span>,<span class="number">49</span>,<span class="number">136</span>,<span class="number">165</span>,<span class="number">205</span>,<span class="number">123</span>,<span class="number">189</span>,<span class="number">45</span>,<span class="number">116</span>,<span class="number">208</span>,<span class="number">18</span>,<span class="number">184</span>,<span class="number">229</span>,<span class="number">180</span>,<span class="number">176</span>,<span class="number">137</span>,<span class="number">105</span>,<span class="number">151</span>,<span class="number">74</span>,<span class="number">12</span>,<span class="number">150</span>,<span class="number">119</span>,<span class="number">126</span>,<span class="number">101</span>,<span class="number">185</span>,<span class="number">241</span>,<span class="number">9</span>,<span class="number">197</span>,<span class="number">110</span>,<span class="number">198</span>,<span class="number">132</span>,<span class="number">24</span>,<span class="number">240</span>,<span class="number">125</span>,<span class="number">236</span>,<span class="number">58</span>,<span class="number">220</span>,<span class="number">77</span>,<span class="number">32</span>,<span class="number">121</span>,<span class="number">238</span>,<span class="number">95</span>,<span class="number">62</span>,<span class="number">215</span>,<span class="number">203</span>,<span class="number">57</span>,<span class="number">72</span>],A=[<span class="number">462357</span>,<span class="number">472066609</span>,<span class="number">943670861</span>,<span class="number">1415275113</span>,<span class="number">1886879365</span>,<span class="number">2358483617</span>,<span class="number">2830087869</span>,<span class="number">3301692121</span>,<span class="number">3773296373</span>,<span class="number">4228057617</span>,<span class="number">404694573</span>,<span class="number">876298825</span>,<span class="number">1347903077</span>,<span class="number">1819507329</span>,<span class="number">2291111581</span>,<span class="number">2762715833</span>,<span class="number">3234320085</span>,<span class="number">3705924337</span>,<span class="number">4177462797</span>,<span class="number">337322537</span>,<span class="number">808926789</span>,<span class="number">1280531041</span>,<span class="number">1752135293</span>,<span class="number">2223739545</span>,<span class="number">2695343797</span>,<span class="number">3166948049</span>,<span class="number">3638552301</span>,<span class="number">4110090761</span>,<span class="number">269950501</span>,<span class="number">741554753</span>,<span class="number">1213159005</span>,<span class="number">1684763257</span>];r.<span class="keyword">exports</span>=&#123;encrypt:function(r,n,t)&#123;<span class="keyword">return</span> l(r,n,<span class="number">1</span>,t)&#125;,decrypt:function(r,n,t)&#123;<span class="keyword">return</span> l(r,n,<span class="number">0</span>,t)&#125;&#125;&#125;&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="4、sm-html"><a href="#4、sm-html" class="headerlink" title="4、sm.html"></a>4、sm.html</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&lt;!doctype html&gt;</span><br><span class="line">&lt;html lang=<span class="string">&quot;zh-cmn-Hans&quot;</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">  &lt;meta charset=<span class="string">&quot;UTF-8&quot;</span>/&gt;</span><br><span class="line">  &lt;title&gt;SM4加解密&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;strong&gt;打开控制台，查看结果&lt;/strong&gt;</span><br><span class="line">&lt;br&gt;脚本来自：&lt;https:<span class="comment">//github.com/JuneAndGreen/sm-crypto&gt;</span></span><br><span class="line">&lt;script src=<span class="string">&quot;sm4.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * SM4-ECB-PKCS5Padding对称加密</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> text &#123;string&#125; 要加密的明文</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> secretKey &#123;string&#125; 密钥，43位随机大小写与数字</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@returns</span> &#123;string&#125; 加密后的密文，Base64格式</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  function <span class="title function_">SM4_ECB_ENCRYPT</span><span class="params">(text, secretKey)</span> &#123;</span><br><span class="line">	<span class="keyword">return</span> sm4.encrypt(text, secretKey).toUpperCase();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * SM4-ECB-PKCS5Padding对称解密</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> textBase64 &#123;string&#125; 要解密的密文，Base64格式</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> secretKey &#123;string&#125; 密钥，43位随机大小写与数字</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@returns</span> &#123;string&#125; 解密后的明文</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  function <span class="title function_">SM4_ECB_DECRYPT</span><span class="params">(textBase64, secretKey)</span> &#123;</span><br><span class="line">	<span class="keyword">return</span> sm4.decrypt(textBase64, secretKey);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">  <span class="type">var</span> <span class="variable">message</span> <span class="operator">=</span> <span class="string">&quot;sm4对称加密&lt;pkcs5&gt;演示←←&quot;</span>;</span><br><span class="line">  <span class="type">var</span> <span class="variable">key</span> <span class="operator">=</span> <span class="string">&quot;FA171555405706F73D7B973DB89F0B47&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="type">var</span> <span class="variable">ecbEncrypt</span> <span class="operator">=</span> SM4_ECB_ENCRYPT(message, key);</span><br><span class="line">  console.log(<span class="string">&quot;ecb加密&quot;</span>, ecbEncrypt);</span><br><span class="line">  <span class="type">var</span> <span class="variable">ecbDecrypt</span> <span class="operator">=</span> SM4_ECB_DECRYPT(ecbEncrypt, key);</span><br><span class="line">  console.log(<span class="string">&quot;ecb解密&quot;</span>, ecbDecrypt);</span><br><span class="line">  console.log(<span class="string">&quot;明文与解密结果比较---&quot;</span>, message === ecbDecrypt)</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>


<script src="/qingfeng-blog/js/vdonate.js"></script>

<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150832.png',
  alipayImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150823.png'
});
</script>
      
      
      <div>
        <ul class="post-copyright">
          <li class="post-copyright-author">
          <strong>Post author:  </strong>清峰</a>
          </li>
          <li class="post-copyright-link">
          <strong>Post link:  </strong>
          <a href="/qingfeng-blog/2021/06/28/Springboot/SM4加解密（前后端）/" target="_blank" title="SM4加解密">http://example.com/2021/06/28/Springboot/SM4加解密（前后端）/</a>
          </li>
          <li class="post-copyright-license">
            <strong>Copyright Notice:   </strong>
            All articles in this blog are licensed under <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
            unless stating additionally.
          </li>
         
        </ul>
<div>

      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/qingfeng-blog/tags/sm4/" rel="tag">sm4</a></li></ul>

      

    </footer>
  </div>
</div>
  
    
<nav id="article-nav">
  
    <a href="/qingfeng-blog/2021/07/08/Mysql/MySql%E5%BC%80%E5%90%AFSSL%E5%AE%89%E5%85%A8%E8%AE%A4%E8%AF%81/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          MySql开启SSL安全认证
        
      </div>
    </a>
  
  
    <a href="/qingfeng-blog/2021/05/23/Nginx/Nginx%E9%85%8D%E7%BD%AEHttps/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Nginx配置Https</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#SM4%E5%8A%A0%E8%A7%A3%E5%AF%86%EF%BC%88%E5%89%8D%E5%90%8E%E7%AB%AF%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">SM4加解密（前后端）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81Sm4%E5%B7%A5%E5%85%B7%E7%B1%BB"><span class="nav-number">1.1.</span> <span class="nav-text">1、Sm4工具类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B7%A5%E5%85%B7%E7%B1%BB"><span class="nav-number">1.2.</span> <span class="nav-text">2、字符串工具类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3%E3%80%81sm-js"><span class="nav-number">1.3.</span> <span class="nav-text">3、sm.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4%E3%80%81sm-html"><span class="nav-number">1.4.</span> <span class="nav-text">4、sm.html</span></a></li></ol></li></ol>
    
    </div>
  </aside>


</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <!-- <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p> -->
	      <p>May there be no bug in the world！</p>
        <p id="copyRightEn">Copyright &copy; 2022 - 2023 All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				UV : <span id="busuanzi_value_site_uv"></span> |  
				PV : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/qingfeng-blog/" class="mobile-nav-link">首页</a>
  
    <a href="/qingfeng-blog/categories" class="mobile-nav-link">分类</a>
  
    <a href="/qingfeng-blog/tags" class="mobile-nav-link">标签</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  
<link rel="stylesheet" href="/qingfeng-blog/fancybox/jquery.fancybox.css">

  
<script src="/qingfeng-blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/qingfeng-blog/js/scripts.js"></script>



  
<script src="/qingfeng-blog/js/home.js"></script>




  
<script src="/qingfeng-blog/js/dialog.js"></script>









	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            清峰小栈
          </div>
          <div class="panel-body">
            Copyright © 2023 清峰 All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/qingfeng-blog/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>